package file;

import java.awt.Component;
import java.io.*;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

import display.Display;
/**
 * 
 * @author gabriella
 *
 */
public class FileHandler  {
	
	private int C[], D[];
	private float F[];
	int index;
	
	public FileHandler() {
		C = new int[501];
		D = new int[501];
		F = new float[501];
		index = 0;
	}
	
	public void saveEPS(Display disp) {
		if (disp != null) {
			JFileChooser fc = new JFileChooser(".") {
				@Override
		        public void approveSelection() {
		            File f = getSelectedFile();
		            if (f.exists() && getDialogType() == SAVE_DIALOG) {
		                int result = JOptionPane.showConfirmDialog(this,
		                        "The file exists, overwrite?", "Existing file",
		                        JOptionPane.YES_NO_CANCEL_OPTION);
		                switch (result) {
		                case JOptionPane.YES_OPTION:
		                    super.approveSelection();
		                    return;
		                case JOptionPane.CANCEL_OPTION:
		                    cancelSelection();
		                    return;
		                default:
		                    return;
		                }
		            }
		            super.approveSelection();
		        }
		    };
			fc.setFileFilter(new FileNameExtensionFilter("Encapsulated PostScript (*.eps)", "eps"));
			int returnVal = fc.showSaveDialog((Component) disp);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				if (!disp.saveScreenShot(fc.getSelectedFile()))
					JOptionPane.showMessageDialog(null,
							"Nem sikerült menteni az EPS fájlt!", "EPS mentés",
							JOptionPane.ERROR_MESSAGE);
			}
		} else
			JOptionPane.showMessageDialog(null,
					"Nem sikerült menteni az EPS fájlt!", "EPS mentés",
					JOptionPane.ERROR_MESSAGE);
	}
	
	public void resetLog() {
		index = 0;
	}
	
	public void record(int c, int d, float f) {
		if (index < 501) {
			C[index] = c;
			D[index] = d;
			F[index++] = f;
		}
	}
	
	public void saveLog(Display disp) {
		JFileChooser fc = new JFileChooser("."){
			@Override
	        public void approveSelection() {
	            File f = getSelectedFile();
	            if (f.exists() && getDialogType() == SAVE_DIALOG) {
	                int result = JOptionPane.showConfirmDialog(this,
	                        "The file exists, overwrite?", "Existing file",
	                        JOptionPane.YES_NO_CANCEL_OPTION);
	                switch (result) {
	                case JOptionPane.YES_OPTION:
	                    super.approveSelection();
	                    return;
	                case JOptionPane.CANCEL_OPTION:
	                    cancelSelection();
	                    return;
	                default:
	                    return;
	                }
	            }
	            super.approveSelection();
	        }
	    };
		fc.setFileFilter(new FileNameExtensionFilter("Log file (*.log)", "log"));
		int returnVal = fc.showSaveDialog((Component) disp);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			try {
				BufferedWriter out = new BufferedWriter(new FileWriter(fc.getSelectedFile()));
				for (int i = 0; i < index; i++) {
					out.write("C = " + C[i] + " D = " + D[i] + " F = " + F[i] + "\n");
				}
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				JOptionPane.showMessageDialog(null,
						"Nem sikerült menteni a log fájlt!", "Statisztika mentése",
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}

}
 
